package com.wxc.util;



import com.wxc.common.File;
import com.wxc.common.User;
import com.wxc.vo.Message;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * @author wxc
 * @date 2021年10月03日 9:26
 */
public class InputUtil {
    private static final Scanner scanner = new Scanner(System.in);
    public static User getUser() {
        User user = new User();
        System.out.println("请输入用户名: ");
        user.setUserName(scanner.nextLine().trim());
        System.out.println("请输入密码: ");
        user.setPassword(scanner.nextLine().trim());
        return user;
    }

    public static String getKey() {
        return scanner.nextLine().trim();
    }

    public static Message<Object> chat(String ownName) {
        System.out.println("请输入要发送消息的对象: ");
        Message<Object> message = new Message<>();
        message.setType(Message.MessageType.CHAT);
        message.setReceiver(scanner.nextLine().trim());
        message.setSender(ownName);
        System.out.println("请输入要发送的消息: ");
        message.setData(scanner.nextLine().trim());
        return message;
    }

    public static Message<Object> chatAll(String ownName) {
        Message<Object> message = new Message<>();
        message.setType(Message.MessageType.CHAT_ALL);
        message.setSender(ownName);
        System.out.println("请输入要发送的消息: ");
        message.setData(scanner.nextLine().trim());
        return message;
    }

    public static Message<Object> sendFile(String ownName) {
        Message<Object> message = new Message<>();
        message.setType(Message.MessageType.FILE);
        message.setSender(ownName);;
        System.out.println("请输入要发送消息的对象: ");
        message.setReceiver(scanner.nextLine().trim());
        System.out.println("请输入要发送的文件路径: ");
        String fileName = scanner.nextLine().trim();
        byte[] bytes;
        try {
            bytes = StreamUtil.streamToByteArray(new BufferedInputStream(new FileInputStream(fileName)));
        } catch (FileNotFoundException e) {
            System.out.println("文件不存在!");
            return null;
        }
        int index = fileName.lastIndexOf(".");
        if(index < 0) message.setData(new File(bytes,fileName,""));
        else {
            String suffix = fileName.substring(index);
            message.setData(new File(bytes,fileName,suffix));
        }
        return message;
    }
}
